const Minio = require('minio')
const server = require('express')()
const jwt = require('express-jwt');

const Secret = '2toZTm48Uouef6rQrHVhnVn4X'

const minioClient = new Minio.Client({
    endPoint: 'localhost',
    port: 9000,
    useSSL: false,
    accessKey: 'minioadmin',
    secretKey: 'minioadmin'
});

// minioClient.makeBucket('mybucket', 'us-east-1', function(err) {
//     if (err) return console.log('Error creating bucket.', err)
//     console.log('Bucket created successfully in "us-east-1".')
//   })

server.get('/presignedUrl', (req, res) => {

    //requestDate needed
    minioClient.presignedPutObject('testnein', req.query.name, (err, url) => {
        if (err) throw err
        res.end(url)
    })
})

/*
{
    "size": 29503,
    "metaData": {
        "content-type": "application/zip"
    },
    "lastModified": "2020-11-14T10:32:40.000Z",
    "etag": "3d4a2a07dab72d6dae33eac97f5c17d5"
}
*/
//etag : md5
server.get('/stat/:id', (req, res) => {
    minioClient.statObject('testnein', req.params.id, function (err, stat) {
        if (err) {
            return res.json(err)
        }
        res.json(stat)
    })
})

server.get('/down/:id', (req, res) => {
    minioClient.presignedGetObject('testnein', req.params.id, 5 * 60, function (err, presignedUrl) {
        if (err) return res.json(err)
        return res.json(presignedUrl)
    })
})

//presignedGetObject(bucketName, objectName[, expiry, respHeaders, requestDate, cb])
/*
minioClient.presignedGetObject('mybucket', 'hello.txt', 24*60*60, function(err, presignedUrl) {
  if (err) return console.log(err)
  console.log(presignedUrl)
})
 */

// removeObject		
// removeObjects		
// removeIncompleteUpload

server.get('/', jwt({ secret: Secret, algorithms: ['HS256'] }), (req, res) => {
    res.json(req.user);
})

var PORT = 2333
server.listen(PORT, () => {
    console.log(PORT)
})